package leetcode.code557.ReverseWordsinaStringIII;

/**
 *
 *
 *
 * @author Administrator
 *
 */
public class Solution4 implements IReverseWordsinaStringIII {

	@Override
	public String reverseWords(String s) {
		if (s == null || s.length() == 0)
			return s;
		char[] res = new char[s.length()];
		for (int i = 0, j = -1; i <= s.length();) {// =Ϊ���һ������׼��
			if (i != s.length() && s.charAt(i) != ' ') {// && ǰΪ���һ�����ʶ����ã���Ȼ�ܲ������һ��
				if (j == -1) {
					j = i;
				}
				i++;
			} else {
				if (j != -1) {
					int r = i;
					int l = j;
					while (j < r) {
						--r;
						res[l] = s.charAt(r);
						l++;
					}
					j = -1;
				}
				if (i != s.length())// ���һ���ո�
					res[i] = ' ';
				i++;
			}
		}
		return new String(res);
	}

	public static void main(String[] args) {
		IReverseWordsinaStringIII i = new Solution4();
		i.debug1(i);
	}

	@Override
	public String debug(String s, IReverseWordsinaStringIII i) {
		System.out.println(i.reverseWords(s));
		return null;
	}

	@Override
	public String debug1(IReverseWordsinaStringIII i) {
//		���룺"Let's take LeetCode contest"
//		�����"s'teL ekat edoCteeL tsetnoc"
		String s = "Let's take  LeetCode contest";
		String r = "s'teL ekat  edoCteeL tsetnoc";
		System.out.println(r);
		this.debug(s, i);
		return null;
	}

	@Override
	public String debug2(IReverseWordsinaStringIII i) {

		return null;
	}

}
